package pub.developers.forum.infrastructure;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Repository;
import pub.developers.forum.common.enums.ErrorCodeEn;
import pub.developers.forum.common.model.PageResult;
import pub.developers.forum.common.support.CheckUtil;
import pub.developers.forum.domain.entity.Comment;
import pub.developers.forum.domain.repository.CommentRepository;
import pub.developers.forum.infrastructure.dal.dao.CommentDAO;
import pub.developers.forum.infrastructure.dal.dao.CommentFollowDAO;
import pub.developers.forum.infrastructure.dal.dao.UserFollowDAO;
import pub.developers.forum.infrastructure.dal.dataobject.CommentDO;
import pub.developers.forum.infrastructure.dal.dataobject.UserFollowDO;
import pub.developers.forum.infrastructure.transfer.CommentTransfer;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;


@Repository
public class CommentRepositoryImpl implements CommentRepository {

    @Resource
    private CommentDAO commentDAO;
    @Resource
    private CommentFollowDAO commentFollowDAO;

    @Resource
    private UserFollowDAO userFollowDAO;


    public void deleteByUserId(Long userId){
        commentDAO.deleteByUserId(userId);
    }

    public void softDelete(Long id,Date update){
        commentDAO.softDelete(id,update);
    }

    @Override
    public void save(Comment comment) {
        CommentDO commentDO = CommentTransfer.toCommentDO(comment);

        commentDAO.insert(commentDO);

        comment.setId(commentDO.getId());
    }

    @Override
    public Comment get(Long id) {
        return CommentTransfer.toComment(commentDAO.findByPrimaryKey(id));
    }

    @Override
    public List<Comment> queryByPostsId(Long postsId) {
        return CommentTransfer.toComments(commentDAO.queryByPostsId(postsId));
    }

    @Override
    public PageResult<Comment> queryInReplyIds(Set<Long> replyIds , Integer pageSize , Integer pageNo) {
        PageHelper.startPage(pageNo , pageSize);
        List<CommentDO> commentDOS = commentDAO.queryInReplyIds(new ArrayList<>(replyIds));
        PageInfo<CommentDO> pageInfo = new PageInfo<>(commentDOS);
        return PageResult.build(pageInfo.getTotal(), pageInfo.getSize(), CommentTransfer.toComments(commentDOS));
    }

    public List<Comment> queryInReplyIdOrReplyReplyId(Long id){
        return CommentTransfer.toComments(commentDAO.queryInReplyIdOrReplyReplyId(id));
    }

    @Override
    public PageResult<Comment> page(Integer pageNo, Integer pageSize, Long postsId) {
        PageHelper.startPage(pageNo, pageSize);

        List<CommentDO> commentDOS = commentDAO.queryByPostsId(postsId);

        PageInfo<CommentDO> pageInfo = new PageInfo<>(commentDOS);

        return PageResult.build(pageInfo.getTotal(), pageInfo.getSize(), CommentTransfer.toComments(commentDOS));
    }

    @Override
    public void deleteByPostsId(Long postsId) {
        commentDAO.deleteByPostsId(postsId);
    }

    @Override
    public List<Comment> queryInIds(Set<Long> ids) {
        List<CommentDO> commentDOS = commentDAO.queryInIds(ids);
        return CommentTransfer.toComments(commentDOS);
    }

    //给评论点赞
    @Override
    public void followCommentRepository(Long userId , Long commentID)
    {
        UserFollowDO userFollowDO = new UserFollowDO();
        userFollowDO.setFollowed(commentID);
        userFollowDO.setFollower(userId);
        userFollowDO.setFollowedType("COMMENTS");
        Date time = new Date();
        userFollowDO.setCreateAt(new Date());
        UserFollowDO tmp = userFollowDAO.get(userId,commentID,"COMMENTS");
        CheckUtil.isNotEmpty(tmp, ErrorCodeEn.REPEAT_OPERATION);
        userFollowDAO.insert(userFollowDO); // 插入点赞数据

        //评论点赞次数加1
        //首先拿到该评论的点赞次数
        Long count = commentDAO.getFollowCountById(commentID);
        if (count == null)
        {
            count = 0L;
        }
        Long newCount = count + 1 ;
        //修改评论点赞次数
        commentDAO.updateFollowCountAdd(commentID , newCount);
    }



    //取消给评论点赞
    public void unfollowCommentRepository(Long userId , Long commentId)
    {
        Long id = userFollowDAO.queryId(userId , commentId,"COMMENTS");
        CheckUtil.isEmpty(id,ErrorCodeEn.OPERATION_DATA_NOT_EXIST);
        userFollowDAO.delete(id);//删除点赞数据
        //首先拿到该评论的点赞次数
        Long count = commentDAO.getFollowCountById(commentId);
        Long newCount = count - 1 ;
        //修改评论点赞次数
        commentDAO.updateFollowCountAdd(commentId , newCount);
    }

    //根据用户id 查询用户的评论id , 该评论所属帖子的帖子id , 该评论的点赞次数
    @Override
    public Long[] getCommentIdByUserid(Long userId) {
        return commentDAO.selectCommentIdByUserid(userId);
    }

    //根据评论id , 去查看是否有人给这条评论点赞 , 如果有 , 拿到点赞的人的id
    @Override
    public Long[] getAllFollower(Long commentID) {
        return commentFollowDAO.selectCommentFollower(commentID);
    }

    @Override
    public List<Comment> queryThreeInReplyIds(Set<Long> replyIds) {
        return CommentTransfer.toComments(commentDAO.queryThreeInReplyIds(new ArrayList<>(replyIds)));
    }

    //统计二级评论个数
    @Override
    public List<Comment> queryInReplyIdsCount(Set<Long> replyIds) {
        return CommentTransfer.toComments(commentDAO.queryInReplyIdsCount(new ArrayList<>(replyIds)));
    }

}
